package com.ybw;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ybw.parseJson.*;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

public class MainInfoResource {
    public static void main(String[] args) {

        try {
            File file=new File("C:\\COC1.1-prod\\JSON2.0\\src\\main\\resources\\data.json");
            FileInputStream fis=new FileInputStream(file);
            byte bytes[]=null;
            int length = fis.available();
            bytes = new byte[length];
            fis.read(bytes);
            fis.close();
            String str =new String(bytes, StandardCharsets.UTF_8);

            Map map1=JSON.parseObject(str,Map.class);
            Map map2=(Map)map1.get("paths");
            //map2是个map，键是url-prefix,值是一坨方法的集合(YbwUrl)

            //根据map2获取YbwUrl列表，并填充每个列表对象的path属性
            List<YbwUrl> ybwUrls=new ArrayList<>();
            for(Object obj : map2.entrySet()){
                //obj是一个url:YbwUrl对象
                Map.Entry entry=(Map.Entry)obj;
                String temp1=String.valueOf(JSON.toJSON(entry.getKey()));
                JSONObject temp2=(JSONObject)JSON.toJSON(entry.getValue());
                YbwUrl ybwUrl=JSON.toJavaObject(temp2,YbwUrl.class);
                ybwUrl.setPath(temp1);
                ybwUrls.add(ybwUrl);
            }

            //根据YbwUrl列表，生成YbwResult列表
            List<YbwResult> ybwResults=new ArrayList<>();
            for(YbwUrl ybwUrl : ybwUrls){
                YbwResult ybwResult=null;
                if(ybwUrl.getPut()!=null){
                    ybwResult=new YbwResult();
                    YbwMethod ybwMethod=ybwUrl.getPut();
                    ybwResult.setPath(ybwUrl.getPath());
                    ybwResult.setMethod("put");
                    ybwResult.setController(ybwMethod.getTags().get(0));
                    ybwResult.setDescription(ybwMethod.getSummary());
                    ybwResults.add(ybwResult);
                }
                if(ybwUrl.getGet()!=null){
                    ybwResult=new YbwResult();
                    YbwMethod ybwMethod=ybwUrl.getGet();
                    ybwResult.setPath(ybwUrl.getPath());
                    ybwResult.setMethod("get");
                    ybwResult.setController(ybwMethod.getTags().get(0));
                    ybwResult.setDescription(ybwMethod.getSummary());
                    ybwResults.add(ybwResult);
                }
                if(ybwUrl.getPost()!=null){
                    ybwResult=new YbwResult();
                    YbwMethod ybwMethod=ybwUrl.getPost();
                    ybwResult.setPath(ybwUrl.getPath());
                    ybwResult.setMethod("post");
                    ybwResult.setController(ybwMethod.getTags().get(0));
                    ybwResult.setDescription(ybwMethod.getSummary());
                    ybwResults.add(ybwResult);
                }
                if(ybwUrl.getDelete()!=null){
                    ybwResult=new YbwResult();
                    YbwMethod ybwMethod=ybwUrl.getDelete();
                    ybwResult.setPath(ybwUrl.getPath());
                    ybwResult.setMethod("delete");
                    ybwResult.setController(ybwMethod.getTags().get(0));
                    ybwResult.setDescription(ybwMethod.getSummary());
                    ybwResults.add(ybwResult);
                }
            }
            //path中所有路径参数改成统一正则表达式[a-zA-Z0-9]{0,64}
            int inta,intb;
            String stra,strb;
            for(YbwResult ybwResult : ybwResults){
                if(ybwResult.getPath().contains("{")){
                    inta=ybwResult.getPath().indexOf("{");
                    intb=ybwResult.getPath().indexOf("}");
                    stra=ybwResult.getPath().substring(0,inta);
                    if(intb+1<ybwResult.getPath().length()) {
                        strb = ybwResult.getPath().substring(intb + 1, ybwResult.getPath().length() - 1);
                    }else{
                        strb="";
                    }
                    ybwResult.setPath(stra+"[a-zA-Z0-9]{0,64}"+strb);
                }
            }

            //根据controller分组
            Map<String,List<YbwResult>> map3=ybwResults.stream().collect(Collectors.groupingBy(YbwResult::getController));
            //根据controller排序
            Map<String,List<YbwResult>> map4=new TreeMap<>(
                    new Comparator<String>() {
                        @Override
                        public int compare(String o1, String o2) {
                            return Integer.valueOf(o1.compareTo(o2));
                        }
                    }
            );
            for(Object obj : map3.entrySet()){
                Map.Entry<String,List<YbwResult>> entry=(Map.Entry)obj;
                map4.put(entry.getKey(),entry.getValue());
            }
            List<YbwResult> ybwResults1=new ArrayList<>();
            for(Object obj : map4.entrySet()){
                Map.Entry<String,List<YbwResult>> entry=(Map.Entry)obj;
                ybwResults1.addAll(entry.getValue());
            }

            //构造mysql语句
            StringBuffer sb=new StringBuffer();
            int resourceId_a=10000;
            int resourceId_b=0;
            String controllerMem="iniStr";
            for(YbwResult ybwResult : ybwResults1){
                if(!controllerMem.equals(ybwResult.getController())){
                    resourceId_a+=100;
                    resourceId_b=1;
                    controllerMem=ybwResult.getController();
                }
                sb.append("INSERT INTO user.`info_resource` VALUES (");
                sb.append(resourceId_a+resourceId_b)
                        .append(",NULL,'")
                        .append(ybwResult.getDescription())
                        .append("',NULL,NULL,1,0,NULL,'")
                        .append(ybwResult.getPath())
                        .append("',NULL,NULL,1,'2021-08-20 15:05:37',1,'2021-08-20 15:05:37',NULL);\n");
                resourceId_b++;
            }

//            System.out.println(sb);

            FileOutputStream fos=new FileOutputStream(new File("C:\\COC1.1-prod\\JSON2.0\\src\\main\\resources\\data2.json"));
            fos.write(sb.toString().getBytes(StandardCharsets.UTF_8));
            fos.close();


            System.out.println("done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
